#include "stdafx.h"
#include "Utility.h"

void GetListOfCDorDVD(CStringArray &arString)
{
    int nArrayCount = arString.GetCount();
    CFileFind CSearchFile;
    UINT unDriveType = 0;
    CStringArray arNew;

    for(int i = 0; i < nArrayCount; ++i)
    {
        unDriveType = GetDriveType(arString[i]);

        if(unDriveType == DRIVE_CDROM)
        {
            arNew.Add(arString[i]);
        }
    }

    arString.RemoveAll();
    arString.Append(arNew);

}

bool GetVolumesList(CStringArray &arDrives)
{
    arDrives.RemoveAll();

    DWORD dwSize = GetLogicalDriveStrings(0, NULL);

    if(dwSize)
    {
        LPWSTR szDrives = new TCHAR[dwSize]; 

        dwSize = GetLogicalDriveStringsW(dwSize, szDrives);

        if (dwSize) 
        {
            CString sDrive;
            UINT i = 0;

            while(i < static_cast<UINT>(dwSize))
            {
                sDrive="";
                while(szDrives[i] != '\0')
                {
                    sDrive += szDrives[i];
                    ++i;
                }

                arDrives.Add(sDrive);

                ++i;
            }
        }

        delete [] szDrives;

        return true;
    }

    return false;
}

bool GetValidVolumes(CStringArray &arString)
{
    int nValidCount = arString.GetCount();
    CStringArray arValidList;
    CFileFind CSearchFile;

    for(int i = 0; i < nValidCount; ++i)
    {
        BOOL bResult = CSearchFile.FindFile(arString[i] + L"*");

        if(bResult)
        {
            arValidList.Add(arString[i]);
        }
    }

    arString.RemoveAll();
    arString.Append(arValidList);

    return true;
}

bool GetFoldersInDirectory(CString &sPath, CStringArray &arFolders)
{
    arFolders.RemoveAll();

    int nCount = sPath.GetLength();

    if(sPath.Find('\\', nCount - 1) == -1)
    {
        sPath += L"\\";
    }

    CString sPathForSearch = sPath + L"*";
    CFileFind CSearchFile;

    BOOL bResult = CSearchFile.FindFile(sPathForSearch);

    if(!bResult)
    {
        return false;
    }

    while(bResult)
    {
        bResult = CSearchFile.FindNextFile();

        if(CSearchFile.IsDirectory())
        {
            if(!CSearchFile.IsDots())
            {
                CString sFolder = CSearchFile.GetFileName();
                
                arFolders.Add(sFolder);
            }
        }

    }

    CSearchFile.Close();

    return true;
}

void DivideStringArrays(CStringArray &arResultArray, CStringArray &arComparedArray)
{
    int nResultArrayCount = arResultArray.GetCount();
    int nComparedArrayCount = arComparedArray.GetCount();

    for(int i = 0; i < nComparedArrayCount; ++i)
    {
        CString sText = arComparedArray[i];

        for(int j = 0; j < nResultArrayCount; ++j)
        {
            if(sText == arResultArray[j])
            {
                arResultArray.RemoveAt(j);
                nResultArrayCount--;
            }
        }

    }
}